<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <link href="../css/element.css" rel="stylesheet"/>
    <link href="../css/styleindex.css" rel="stylesheet"/>
    <link href="../css/swiper.min.css" rel="stylesheet"/>
    <style>
    .x-ico-month2 .el-input__inner{
      border: 0;
      font-size: 1rem;
      text-align: center;
    }.x-ico-month2 .el-input__prefix{
      display: none;
    }
    </style>
</head>
<body style="background:#EFF0F4;">

  <div class="mui-content">
    <div id="vm" class="x-examok" v-cloak>
      <!--  -->
      <div class="x-ico-month x-ico-month2" style="box-shadow:none;margin:1rem;border-radius:.4rem;">
        <el-date-picker v-model="cardsmonth" type="date" placeholder="" @change="getdata" format="yyyy 年 MM 月 dd 日" value-format="yyyyMMdd" :editable="notkey"></el-date-picker>
      </div>
      <!--  -->
      <div class="x-workforce-tabs swiper-container" style="margin:1rem">
        <div class="swiper-wrapper">
          <div class="swiper-slide x-workforce-items" v-for="(item,index) in formheaderdata" :key="item.id" @click="changetab(index,item.id)" :class="{active:curId==index}">{{item.title}}</div>
        </div>
      </div>
      <div class="x-pro-top" style="margin:0 1rem">预估营业额：{{money}}</div>
      <!--  -->
      <div class="x-pro-list" v-for="(itemb,indexb) in formdata" :key="itemb.key" v-if="formdata!=''">
        <div class="x-pro-top">工作站：{{itemb.title}}</div>
        <div class="">
          <!--  -->
          <div class="x-wf-sif" v-for="(itemk,index) in itemb.list" :key="itemk.key" v-if="parseInt(itemk.workid)>'0'">
            <div class="x-pro-row">
              <div class="x-pro-label">计划工时：</div>
              <div class="x-pro-input">
                {{itemk.date1}}-{{itemk.date2}}
              </div>
            </div>
            <div class="x-pro-row">
              <div class="x-pro-label">排班工时：</div>
              <div class="x-pro-input" style="display:flex;">
                <el-time-select placeholder="起始时间" v-model="itemk.before_date1" :picker-options="{start: '04:00',step: '00:30',end: '24:00'}" :disabled="isfromindex" :editable="notkey"></el-time-select>
                <div style="align-self:center;">-</div>
                <el-time-select placeholder="结束时间" v-model="itemk.before_date2" :picker-options="{start: '04:00',step: '00:30',end: '24:00',minTime: itemk.before_date1}" :disabled="isfromindex" :editable="notkey"></el-time-select>
              </div>
            </div>
            <div class="x-pro-row">
              <div class="x-pro-label">排班员工：</div>
              <div class="x-pro-input">
                <el-select v-model="itemk.memberid" placeholder="请选择" :disabled="isfromindex">
                  <el-option v-for="item in persondata" :key="item.id" :label="item.username" :value="item.id"></el-option>
                </el-select>
              </div>
            </div>
          </div>
          <!-- 手动排班列表  workid=0 -->
          <div class="x-wf-hand" v-for="(itemk,index) in itemb.list" v-if="itemk.workid=='0'">
            <div class="x-pro-row">
              <div class="x-pro-label">排班工时：</div>
              <div class="x-pro-input" style="display:flex;">
                <el-time-select placeholder="起始时间" v-model="itemk.before_date1" :picker-options="{start: '04:00',step: '00:30',end: '24:00'}" :disabled="isfromindex" :editable="notkey"></el-time-select>
                <div style="align-self:center;">-</div>
                <el-time-select placeholder="结束时间" v-model="itemk.before_date2" :picker-options="{start: '04:00',step: '00:30',end: '24:00',minTime: itemk.before_date1}" :disabled="isfromindex" :editable="notkey"></el-time-select>
              </div>
            </div>
            <div class="x-pro-row">
              <div class="x-pro-label">排班员工：</div>
              <div class="x-pro-input">
                <el-select v-model="itemk.memberid" placeholder="请选择" :disabled="isfromindex">
                  <el-option v-for="item in persondata" :key="item.id" :label="item.username" :value="item.id"></el-option>
                </el-select>
              </div>
            </div>
            <div class="x-pro-remove" @click="removeentrywork(indexb,index)" v-if="!isfromindex">删除</div>
          </div>

          <div class="x-pro-addbox" @click="addhandwork(itemb.list,indexb)" v-if="!isfromindex"><span class="x-pro-addicon"></span>手动添加排班</div>
        </div>
      </div>
      <!-- 空空如也 -->
      <div class="x-nothing" v-if="formdata==''">空空如也</div>
      <!--  -->
      <div class="x-pro-gobtn" @click="isok" v-if="!isfromindex">提交</div>
    </div>
  </div>

  <script type="text/javascript" src="../script/vue.js"></script>
  <script type="text/javascript" src="../script/element.js"></script>
  <script type="text/javascript" src="../script/api.js"></script>
  <script type="text/javascript" src="../script/base.js"></script>
  <script type="text/javascript" src="../script/swiper.min.js"></script>
	<script>
apiready = function () {



    //
    var vm = new Vue({
      el:'#vm',
      data(){
        return{
          cardsmonth:'',
          tabsdata:'',
          isfromindex:false,
          persondata:'',
          formdata:'',
          formheaderdata:'',
          curId:0,
          tabsid:0,
          money:'',
          personlimits:'',//人的权限 1有权限 else没权限
          datalimits:'',//数据的权限 1有权限 else没权限
          notkey:false,



        }
      },
      created:function() {
        var that=this;


      },
      mounted:function(){
        var that=this;
        //当前月份
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth()+1;
        var day = date.getDate()
        if(month<10){
          month = '' + 0 + month;
        }
        if(day<10){
          day = '' + 0 + day;
        }
        var toMonth = '' + year +  month +  day;
        that.cardsmonth = toMonth;
        that.getpersondata()
        that.getdata()

      },
      methods: {
        //手动添加排班
        addhandwork:function(data,index){
          var that = this;
          // console.log(JSON.stringify(data));
          that.formdata[index].list.push({
            'workid':0,
            'before_date1':'',
            'before_date2':''
          })
          // console.log(JSON.stringify(that.formdata));

        },
        getpersondata:function(){
          let that=this;
          api.ajax({
            url:baseurl+'api/person/list',
            methods:'get',
            data:{
              values:{
                sec: $api.getStorage('apptoken')
              }
            }
          },function(ret,err){
            if(ret){
              api.hideProgress();
              // console.log(JSON.stringify(ret));
              if(ret.status == 1){
                that.persondata = ret.data.list;

              }else{
                api.toast({
                    msg: ret.msg,
                    duration: 3000,
                    location: 'bottom'
                });
              }
            }else{
              api.hideProgress();
              console.log(JSON.stringify(err));
              api.toast({
                  msg: err.msg,
                  duration: 3000,
                  location: 'bottom'
              });
            }
          })
        },
        //
        getdata:function(){
          let that=this;
          api.ajax({
            url:baseurl+'api/workforce/show',
            methods:'get',
            data:{
              values:{
                sec: $api.getStorage('apptoken'),
                areaid:that.tabsid,
                date:that.cardsmonth
              }
            }
          },function(ret,err){
            if(ret){
              api.hideProgress();
              console.log(JSON.stringify(ret));
              if(ret.status == 1){
                  that.formheaderdata = ret.data.area;
                  that.personlimits = ret.data.access;
                  that.datalimits = ret.data.is_edit;
                  if(that.personlimits=='1'&&that.datalimits=='1'){//1有权限 0没有
                      console.log('有权限');
                      that.isfromindex = false;
                  }else{
                    that.isfromindex = true;
                  }
                  // that.tabsid = ret.data.area[0].id;
                  that.formdata = ret.data.list;
                  that.money = ret.data.predict;
                  that.$nextTick(function(){
                    var mySwiper = new Swiper('.swiper-container',{
                      freeMode: true,
                      loop:false,//设置 active slide 居中后，会有左右留白现象，添加此会让未尾的导航补齐前后空白
                      slideToClickedSlide: true,//设置为true则点击slide会过渡到这个slide。
                      slidesPerView: 2.5,
                      centeredSlides: false,//设定为true时，active slide会居中，而不是默认状态下的居左。
                      spaceBetween: 10,//slide之间设置距离
                      slidesOffsetBefore: 0,//设定slide与左边框的预设偏移量
                      slidesOffsetAfter: 0,//设定slide与右边框的预设偏移量

                    });

                  });
              }else{
                api.toast({
                    msg: ret.msg,
                    duration: 3000,
                    location: 'bottom'
                });
              }
            }else{
              api.hideProgress();
              console.log(JSON.stringify(err));
              api.toast({
                  msg: err.msg,
                  duration: 3000,
                  location: 'bottom'
              });
            }
          })
        },
        //
        isok:function(){
          var that = this;
          console.log(that.cardsmonth);
          console.log(that.tabsid);
          console.log(JSON.stringify(that.formdata));
          api.ajax({
            url:baseurl+'api/workforce/add',
            methods:'get',
            data:{
              values:{
                sec: $api.getStorage('apptoken'),
                date:that.cardsmonth,
                areaid:that.tabsid,
                work_json:JSON.stringify(that.formdata)
              }
            }
          },function(ret,err){
            if(ret){
              api.hideProgress();
              console.log(JSON.stringify(ret));
              if(ret.status == 1){
                api.alert({
                    title: '提交成功',
                    msg: '',
                }, function(ret, err) {
                  that.getdata()
                });

              }else{
                api.toast({
                    msg: ret.msg,
                    duration: 3000,
                    location: 'bottom'
                });
              }
            }else{
              api.hideProgress();
              console.log(JSON.stringify(err));
              api.toast({
                  msg: err.msg,
                  duration: 3000,
                  location: 'bottom'
              });
            }
          })
        },
        //切换选项卡 表格数据
        changetab:function(index,id){
          var that = this;
          that.curId = index;
          console.log(id);
          that.tabsid = id;
          console.log(that.tabsid);
          that.getdata()
        },
        //删除
        removeentrywork:function(indexb,index){
          let that = this;
          var nowin = index;
          console.log(JSON.stringify(that.formdata));
          api.confirm({
            title: '操作提示',
            msg: '确定要删除吗？',
            buttons: ['确定','取消']
          }, function(ret, err) {
            var index = ret.buttonIndex;
            if(index == 1){
              //删除
              that.formdata[indexb].list.splice(nowin,1);//删除全部数组中对应的值
              console.log(JSON.stringify(that.formdata));
            }
          });
        },
      }
    })
	}


	</script>
</body>
</html>
